<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery-1.12.3.js"></script>
		<script>
			$(function(){
				
				var params = location.search;
				//console.log(params); //?id=102
				
				var id =  fn(params, "id");
				//console.log(id);
				
				//
				$.get("json/goods.json", function(d){
					var arr = d;
					for (var i=0; i<arr.length; i++) {
						var obj = arr[i]; 
						if (obj.id == id) {
							fn2(obj);
						}
					}
				})
				
				function fn2(obj){
					console.log( JSON.stringify(obj) );
					
					$("img").attr("src", obj.img);
					$("p").eq(0).html(obj.name); 
					$("p").eq(1).html(obj.unit + obj.price); 
				}
				
				
				//得到指定的某个参数
				function fn(params, id){
					// ?id=102&name=zhang&pwd=123456
					params = params.substring(1); //id=102&name=zhang&pwd=123456
					var arr = params.split("&");
					for (var i=0; i<arr.length; i++) {
						var str = arr[i]; //"id=102" , name=zhang, pwd=123456
						var arr2 = str.split("=");
						if (arr2[0] == id) {
							return arr2[1];
						}
					}
					return "";
				}
				
			})
		</script>
	</head>
	<body>
		<h2>商品详情</h2>
		<img />
		<p>1</p>
		<p>2</p>
	</body>
</html>
